package org.ciallo.simplemusicplayer.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import org.ciallo.simplemusicplayer.R;

/**
 * @author Jukomu
 * @Description:
 * @Package: org.ciallo.simplemusicplayer.fragment
 * @Project: SimpleMusicPlayer
 * @name: LocalMusicContainerFragment
 * @Date: 2025/5/28-02:07
 * @Filename: LocalMusicContainerFragment
 */
public class LocalMusicContainerFragment extends Fragment {

    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    private LocalMusicPagerAdapter pagerAdapter;

    // 确保这些字符串在 strings.xml 中定义
    private final String[] tabTitles = new String[]{
            "单曲", "歌手", "专辑", "文件夹"
    };

    public LocalMusicContainerFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_local_music_container, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tabLayout = view.findViewById(R.id.tabLayout_local_music);
        viewPager = view.findViewById(R.id.viewPager_local_music_content);

        pagerAdapter = new LocalMusicPagerAdapter(requireActivity());
        viewPager.setAdapter(pagerAdapter);

        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> tab.setText(tabTitles[position])
        ).attach();
    }

    private static class LocalMusicPagerAdapter extends FragmentStateAdapter {
        private static final int NUM_TABS = 4;

        public LocalMusicPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            // 为每个tab创建占位Fragment，稍后替换为实际的列表Fragment
            switch (position) {
                case 0:
                    return new SongListFragment();
                case 1:
                    return new ArtistListFragment();
                case 2:
                    return new AlbumListFragment();
                case 3:
                    return new FolderListFragment();
                default:
                    // 理论上不应该发生，但作为防御性编程，返回一个默认或错误提示Fragment
                    return PlaceholderFragment.newInstance("未知页面");
            }
        }

        @Override
        public int getItemCount() {
            return NUM_TABS;
        }
    }

    // 临时的占位 Fragment
    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_CONTENT = "content";
        private String content;

        public PlaceholderFragment() {}

        public static PlaceholderFragment newInstance(String content) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putString(ARG_CONTENT, content);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {
                content = getArguments().getString(ARG_CONTENT);
            }
        }

        @Nullable @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            TextView tv = new TextView(getContext());
            tv.setText(content != null ? content : "占位页面");
            tv.setTextSize(20);
            tv.setGravity(android.view.Gravity.CENTER);
            // 为TextView设置一些内边距，使其不那么靠近边缘
            int padding = (int) (16 * getResources().getDisplayMetrics().density); // 16dp
            tv.setPadding(padding, padding, padding, padding);
            return tv;
        }
    }
}
